package com.issue.system.controller;

import com.issue.common.annotation.Log;
import com.issue.common.core.controller.BaseController;
import com.issue.common.core.domain.AjaxResult;
import com.issue.common.core.domain.entity.SysUser;
import com.issue.common.core.page.TableDataInfo;
import com.issue.common.enums.BusinessType;
import com.issue.common.utils.poi.ExcelUtil;
import com.issue.system.domain.AChildInfo;
import com.issue.system.domain.DailySummary;
import com.issue.system.mapper.AChildInfoMapper;
import com.issue.system.mapper.DailySummaryMapper;
import com.issue.system.mapper.SysUserMapper;
import com.issue.system.service.IDailySummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * SummaryController
 * 
 * @author system
 * @date 2024-10-20
 */
@RestController
@RequestMapping("/system/summary")
public class DailySummaryController extends BaseController
{
    @Autowired
    private IDailySummaryService dailySummaryService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private AChildInfoMapper childInfoMapper;

    @Resource
    private DailySummaryMapper dailySummaryMapper;

    /**
     * 查询Summary列表
     */
    @PreAuthorize("@ss.hasPermi('system:summary:list')")
    @GetMapping("/list")
    public TableDataInfo list(DailySummary dailySummary)
    {
        startPage();
        List<DailySummary> list = dailySummaryService.selectDailySummaryList(dailySummary);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:summary:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(@RequestParam("phone") String phone)
    {
        List<DailySummary> list = dailySummaryMapper.selectListByPhone(phone);
        return getDataTable(list);
    }

    /**
     * 导出Summary列表
     */
    @PreAuthorize("@ss.hasPermi('system:summary:export')")
    @Log(title = "Summary", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DailySummary dailySummary)
    {
        List<DailySummary> list = dailySummaryService.selectDailySummaryList(dailySummary);
        ExcelUtil<DailySummary> util = new ExcelUtil<DailySummary>(DailySummary.class);
        util.exportExcel(response, list, "Summary数据");
    }

    /**
     * 获取Summary详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:summary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dailySummaryService.selectDailySummaryById(id));
    }

    /**
     * 新增Summary
     */
    @PreAuthorize("@ss.hasPermi('system:summary:add')")
    @Log(title = "Summary", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DailySummary dailySummary)
    {

        Long teacherId = dailySummary.getTeacherId();
        Long childId = dailySummary.getChildId();

        if (teacherId == null || childId == null) {
            return AjaxResult.error("Error Message");
        }

        SysUser teacher = sysUserMapper.selectUserById(teacherId);
        if (teacher == null){
            return AjaxResult.error("Error Teacher");
        }
        dailySummary.setTeacherName(teacher.getUserName());

        AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
        if (childInfo == null){
            return AjaxResult.error("Error Child");
        }
        dailySummary.setChildCardNo(childInfo.getChildCardNo());
        dailySummary.setChildName(childInfo.getChildName());
        dailySummary.setGuardianPhone(childInfo.getGuardianPhone());

        return toAjax(dailySummaryService.insertDailySummary(dailySummary));
    }

    /**
     * 修改Summary
     */
    @PreAuthorize("@ss.hasPermi('system:summary:edit')")
    @Log(title = "Summary", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DailySummary dailySummary)
    {

        Long teacherId = dailySummary.getTeacherId();
        Long childId = dailySummary.getChildId();

        if (teacherId != null) {
            SysUser teacher = sysUserMapper.selectUserById(teacherId);
            if (teacher != null){
                dailySummary.setTeacherName(teacher.getUserName());
            }
        }

        if (childId != null) {
            AChildInfo childInfo = childInfoMapper.selectAChildInfoByChildId(childId);
            if (childInfo != null){
                dailySummary.setChildCardNo(childInfo.getChildCardNo());
                dailySummary.setChildName(childInfo.getChildName());
                dailySummary.setGuardianPhone(childInfo.getGuardianPhone());
            }
        }


        return toAjax(dailySummaryService.updateDailySummary(dailySummary));
    }

    /**
     * 删除Summary
     */
    @PreAuthorize("@ss.hasPermi('system:summary:remove')")
    @Log(title = "Summary", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dailySummaryService.deleteDailySummaryByIds(ids));
    }
}
